<template>
  <el-dialog :title="this.id ? '修改': '添加'" :visible.sync="value" :before-close="cancel">
    <el-form :model="dialogForm" :rules="rules" ref="dialogForm" label-width="80px">
      <el-form-item label="上级部门" prop="pid">
        <Treeselect
          v-model="pid"
          placeholder="请选则上级部门"
          :options="departments"
          :normalizer="normalizer"
        ></Treeselect>
      </el-form-item>
      <el-form-item label="部门名称" prop="dname">
        <el-input v-model="dialogForm.dname" placeholder="请输入部门名称" size="small"></el-input>
      </el-form-item>
      <el-form-item label="部门状态" size="small">
        <el-switch
          v-model="dialogForm.status"
          :active-value="1"
          :inactive-value="0"
          active-color="#13ce66"
          inactive-color="#ff4949"
        ></el-switch>
      </el-form-item>
      <el-form-item label="部门描述" prop="descript">
        <el-input type="textarea" placeholder="请输入部门描述" size="small" v-model="dialogForm.descript"></el-input>
      </el-form-item>
    </el-form>
    <div slot="footer">
      <el-button @click="cancel" size="small">取 消</el-button>
      <el-button type="primary" @click="submit" size="small">确 定</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { save, update } from "@/api/department";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";
import Treeselect from "@riophae/vue-treeselect";
export default {
  name: "departmentAddAndUpddate",
  components: { Treeselect },
  props: {
    value: Boolean,
    departments: {
      type: Array,
      default: []
    }
  },
  data() {
    return {
      id: null,
      pid: null,
      dialogForm: {
        pid: null,
        dname: "",
        status: 1,
        descript: ""
      },
      rules: {
        dname: [{ required: true, message: "请输入部门名称", trigger: "blur" }],
        descript: [
          { required: true, message: "请输入部门描述", trigger: "blur" }
        ]
      }
    };
  },
  methods: {
    init(row) {
      this.id = row.id;
      if (this.id) {
        this.pid = row.pid == 0 ? null : row.pid;
        // 进行查询操作
        this.dialogForm.id = row.id;
        this.dialogForm.dname = row.dname;
        this.dialogForm.status = row.status;
        this.dialogForm.descript = row.descript;
      }
    },
    submit() {
      this.$refs["dialogForm"].validate(async isOk => {
        if (isOk) {
          this.dialogForm.pid = this.pid;
          if (this.id) {
            this.dialogForm.id = this.id;
            await update(this.dialogForm);
          } else {
            await save(this.dialogForm);
          }
          // 重新刷新数据
          this.$emit("refresh");
        }
      });
    },
    cancel() {
      this.$refs["dialogForm"].resetFields();
      this.$emit("input", false);
    },
    normalizer(node) {
      //去掉children=[]的children属性
      if (node.children && !node.children.length) {
        delete node.children;
      }
      return {
        id: node.id,
        //将name转换成必填的label键
        label: node.dname,
        children: node.children
      };
    }
  }
};
</script>

<style lang="scss" scoped>
</style>